package com.cskaoyan.utils;


import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Properties;

/**
 * 创建日期: 2022/06/30 09:35
 *
 * @author ciggar
 */
public class JDBCUtils {

    static String url;
    static String username;
    static String password;
    static String driverClassName;


    static {

        Properties properties = new Properties();
        try {
            properties.load(new FileInputStream("jdbc.properties"));
        } catch (IOException e) {
            e.printStackTrace();
        }

        url = properties.getProperty("url");
        username = properties.getProperty("username");
        password = properties.getProperty("password");
        driverClassName = properties.getProperty("driverClassName");
    }


    // 获取连接
    public static Connection getConnection(){

        Connection connection = null;
        try {

            // 注册驱动
            // DriverManager.registerDriver(new Driver());
            // new Driver();
            Class.forName(driverClassName);


            // 获取连接
            connection = DriverManager.getConnection(url,username,password);


        }catch (Exception ex) {
            ex.printStackTrace();
        }

        return connection;

    }

    // 关闭资源
    public static void releaseSources(Connection connection, Statement statement, ResultSet resultSet){
        try {
            if (resultSet != null) resultSet.close();
            if (statement != null) statement.close();
            if (connection != null) connection.close();
        }catch (Exception ex) {
            ex.printStackTrace();
        }
        // 指令重排 volatile
//        int m = 0;
//        int n = 1;
//        int i = m+n;
    }



}
